package yxy.game.pm2.module.union.war;

import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.task;
import yxy.cherry.data.bean.unionfightachievement;
import yxy.cherry.data.script.fomula;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.PlayerModule;
import yxy.game.pm2.bean.module.RedisLock;
import yxy.game.pm2.bean.task.Task;

import java.util.ArrayList;
import java.util.Map;

public class UnionWarPlayerModule extends PlayerModule {
	// bean==========================================================================

	private int times;// 挑战次数
	private int star;// 获得的星级

	ArrayList<String> achievement = new ArrayList<>();

	public int getTimes() {
		return times;
	}

	public void setTimes(int times) {
		this.times = times;
	}

	public int getStar() {
		return star;
	}

	public void setStar(int star) {
		this.star = star;
	}

	public ArrayList<String> getAchievement() {
		return achievement;
	}

	public void setAchievement(ArrayList<String> achievement) {
		this.achievement = achievement;
	}
	// func==========================================================================

	public int times() {
		int times = fomula.联盟站挑战次数.intValue();
		return times - getTimes();
	}

	// redis=========================================================================

	private static String key(String playerCode) {
		return String.format("PLAYER:%s:MODULE:UNIONWAR", playerCode);
	}

	public void set() {
		try {
			setv(key(getPlayerCode()), this);
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	static public UnionWarPlayerModule get(String playerCode) {
		try {
			UnionWarPlayerModule module = getv(key(playerCode));
			if (module == null) {
				module = MySQLCacheDAO.getV(key(playerCode), UnionWarPlayerModule.class);
				if(module != null){module.set();}//同步到redis
			}

			if (module == null) {
				module = create(playerCode);
			}
			if (module.getAchievement().size() <= 0) {
				Map<String, unionfightachievement> alldata = DataCenter.getAllData(unionfightachievement.class);
				for (unionfightachievement data : alldata.values()) {
					task taskdata = DataCenter.getData(data.getTaskid(), task.class);
					Task task = Task.create(playerCode, taskdata);
					module.getAchievement().add(task.getCode());
				}
				module.set();
			}
			return module;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	static public UnionWarPlayerModule create(String playerCode) {
		UnionWarPlayerModule module = new UnionWarPlayerModule();
		module.setPlayerCode(playerCode);
		// 成就
		Map<String, unionfightachievement> alldata = DataCenter.getAllData(unionfightachievement.class);
		for (unionfightachievement data : alldata.values()) {
			task taskdata = DataCenter.getData(data.getTaskid(), task.class);
			Task task = Task.create(playerCode, taskdata);
			module.getAchievement().add(task.getCode());
		}

		return module;
	}

	//存储到数据库的方法
	public static void saveToDB(String playerCode) {
		try {
			String key = key(playerCode);
			if (touchBucket(key).isExists()) {
				UnionWarPlayerModule unionWarPlayerModule = getv(key(playerCode));
				if(unionWarPlayerModule != null)MySQLCacheDAO.setV(key, unionWarPlayerModule);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	//从redis移走数据的操作
	public static void removeAllFromRedis(String playerCode) {
		try {
			touchBucket(key(playerCode)).delete();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	// lcok==========================================================================

	@Override
	public String lockKey() {
		return String.format("lock:%s", key(getPlayerCode()));
	}
	
	@Override
	public boolean tryLock() throws InterruptedException {
		return super.tryLock();
	}

	public static RedisLock lock(String playerCode) {
		return getLock(String.format("lock:%s", key(playerCode)));
	}

}
